package com.hy.demo.ui.list;

import android.graphics.Color;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;

import androidx.appcompat.content.res.AppCompatResources;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hy.demo.R;
import com.hy.demo.adapter.RcyAdapter;
import com.hy.demo.app.BaseActivity;
import com.hy.frame.adapter.IAdapterListener;
import com.hy.frame.recycler.GridItemDecoration;
import com.hy.frame.util.ResUtil;
import com.hy.frame.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * title RcyGridActivity
 * author HeYan
 * time 2021/2/4 9:55
 * desc 无
 */
public class RcyGridActivity extends BaseActivity implements IAdapterListener<String> {
    private RecyclerView rcyList;
    private List<String> datas;
    private RcyAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.v_rcy;
    }

    @Override
    protected void initView() {
        rcyList = findViewById(R.id.rcy_rcyList);
        if (rcyList == null) return;
        rcyList.setLayoutManager(new GridLayoutManager(getContext(), 3));
    }

    @Override
    protected void initData() {
        setTitle(R.string.menu_rcy_grid);
        configBack();
        getTemplate().setHeaderRightTxt("菜单");
        requestData();
    }

    public void requestData() {
        datas = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            datas.add("测试" + (i + 1));
        }
        updateUI();
    }

    public void updateUI() {
        if (adapter == null) {
            adapter = new RcyAdapter(getContext(), datas, this);
            rcyList.setAdapter(adapter);
        } else
            adapter.refresh(datas);
    }


    @Override
    public void onViewClick(View v, String item, int position) {
        ToastUtil.show(this, item);
    }

    private PopupMenu menu;

    @Override
    public void onRightClick() {
        if (menu == null) {
            View v = getTemplate().getHeaderRight();
            menu = new PopupMenu(getContext(), v);
            menu.inflate(R.menu.m_rcy_grid);
            menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    GridItemDecoration decoration = getItemDecoration();
                    switch (item.getItemId()) {
                        case R.id.vMenuAdd:
                            datas.add("测试" + (datas.size() + 1));
                            adapter.refresh();
                            return false;
                        case R.id.vMenuPaddingLeft:
                            if (decoration.getPadding().left > 0)
                                decoration.setLeft(0);
                            else
                                decoration.setLeft(padding);
                            break;
                        case R.id.vMenuPaddingTop:
                            if (decoration.getPadding().top > 0)
                                decoration.setTop(0);
                            else
                                decoration.setTop(padding);
                            break;
                        case R.id.vMenuPaddingRight:
                            if (decoration.getPadding().right > 0)
                                decoration.setRight(0);
                            else
                                decoration.setRight(padding);
                            break;
                        case R.id.vMenuPaddingBottom:
                            if (decoration.getPadding().bottom > 0)
                                decoration.setBottom(0);
                            else
                                decoration.setBottom(padding);
                            break;
                        case R.id.vMenuDivider:
                            if (decoration.getDivider() > 0)
                                decoration.setDivider(0);
                            else
                                decoration.setDivider(padding);
                            break;
                        case R.id.vMenuHorizontalDivider:
                            if (decoration.getVerticalDivider() > 0)
                                decoration.setVerticalDivider(0);
                            else
                                decoration.setVerticalDivider(padding);
                            break;
                        case R.id.vMenuDividerTrans:
                            decoration.setDrawable(null);
                            decoration.setColor(Color.TRANSPARENT);
                            break;
                        case R.id.vMenuDividerColor:
                            decoration.setDrawable(null);
                            decoration.setColor(Color.YELLOW);
                            break;
                        case R.id.vMenuDividerDraw:
                            decoration.setDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.shape_divider));
                            decoration.setColor(Color.TRANSPARENT);
                            break;
                    }
                    adapter.refresh();
                    return false;
                }
            });
        }
        menu.show();
    }

    private GridItemDecoration decoration;
    private int padding;

    private GridItemDecoration getItemDecoration() {
        if (decoration == null) {
            padding = ResUtil.dip2px(getContext(), 10);
            decoration = new GridItemDecoration(0);
            rcyList.addItemDecoration(decoration);
        }
        return decoration;
    }

}
